﻿// See https://aka.ms/new-console-template for more information
public class Node<T>
{
    public Node<T> Next { get; set; }
    public T Data { get; set; }

    public Node(T t)
    {
        Next = null;
        Data = t;
    }
}

public class GenericList<T>
{
    private Node<T> head;
    private Node<T> tail;

    public GenericList() {
        tail = head = null;
    }
    public Node<T> Head {
        get => head;
    }
    public void Add(T t)
    {
        Node<T> n = new Node<T>(t);
        if (tail == null)
        {
            head = tail = n;
        }
        else
        {
            tail.Next = n;
            tail = n;
        }
    }

    public void Cal(Action<T>action) 
    {
        Node<T> cur = head;
        while(cur != tail)
        {
           action(cur.Data);
            cur = cur.Next;
        }
    }
}

public class Test
{
    static void Main(string[] args)
    {
        int sum = 0;
        int max = 0;
        int min = 0;

        Random random = new Random();
        GenericList<int> Glist = new GenericList<int>();
        for(int i = 0; i < 10; i++)
        {
            int r = random.Next(100);
            Glist.Add(r);
        }
        Glist.Cal(x => Console.WriteLine(x));//输出链表元素
        //输出最大值
        Glist.Cal(x => {
            if (max < x)
            {
                max = x;
            }
        });
        //输出最小值
        Glist.Cal(x => {
            if(min > x)
            {
                min = x;
            }
        });
        //输出和
        Glist.Cal(x => {
            sum += x;
        });
    }
}
